const Mock = require('mockjs')
const bodyParer = require('body-parser')

const data = Mock.mock({
  'shopMall|10': [
    {
      id: '@id()',
      'classifyType|+1': ['推荐', '食品', '手机', '男装', '女装', '电器', '健康', '生鲜', '百货', '洗护'],
      'children|20': [
        {
          id: '@guid()',
          count: 0,
          title: '@ctitle()', //
          desc: '@cword(30)', // 车辆描述
          price: '@integer(20, 100)', // 车系价格
          img: '@image(150x150, @color(), @city())',
          classifyType: '@cword(4)',
          'children|20': [
            {
              id: '@id()',
              count: 0,
              collect: false, // 收藏
              title: '@ctitle()', //
              desc: '@cword(30)', //
              price: '@integer(20, 100)', //
              img: '@image(150x150, @color(), @city())',
              classifyType: '@cword(4)',
            },
          ],
        },
      ],
    },
  ],
})

module.exports = (app) => {
  app.use(bodyParer.json())

  // 登陆接口
  app.post('/api/login', (req, res) => {
    const { username, password } = req.body
    res.json({
      code: 0,
      msg: 'success',
      data: { token: `${username}_admin_${password}` },
    })
  })

  // 列表接口
  app.get('/api/shopList', (req, res) => {
    // const { page = 1, size = 5 } = req.query
    res.json({
      code: 0,
      msg: 'success',
      data: { total: data.shopMall.length, list: data.shopMall },
    })
  })

  // 详情接口
  app.get('/api/detail', (req, res) => {
    const { id, tab, tabIndex } = req.query

    res.json({
      code: 0,
      msg: 'success',
      data: data.shopMall[tab].children[tabIndex].children.filter((v) => v.id == id),
    })
  })
}
